package com.minivision.sdktestdemo.utils;

import android.os.Environment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/25.
 */
public class LrcUtils {
    private static volatile LrcUtils sLrc;
    public static final String TEST_LRC_PATH = "";
    private List<String> mLrcList;
    private List<Long> mTimeList;

    private LrcUtils() {
        mLrcList = new ArrayList<>();
        mTimeList = new ArrayList<>();
    }

    public static LrcUtils getInstance() {
        if (sLrc == null) {
            synchronized (LrcUtils.class) {
                if (sLrc == null) {
                    sLrc = new LrcUtils();
                }
            }
        }
        return sLrc;
    }

    public void loadLrc(String path) {
        File file = new File(path);
        loadLrc(file);
    }

    public List<String> getLrcList() {
        return mLrcList;
    }

    public List<Long> getTimeList() {
        return mTimeList;
    }

    public void loadLrcContent(String content) {
        if (!content.isEmpty()) {
            BufferedReader reader = new BufferedReader(new StringReader(content));
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    int start = line.indexOf('[');
                    int end = line.lastIndexOf(']');
                    if (start != -1 && end != -1) {
                        mLrcList.add(line.substring(end + 1));
                        mTimeList.add(getMillSeconds(line.substring(start + 1, end)));
                    }
                }
                int coutn = 0;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                CloseUtils.close(reader);
            }
        }
    }

    public void loadLrc(File file) {
        if (file.exists()) {
            mLrcList.clear();
            mTimeList.clear();
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(file));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    int start = line.indexOf('[');
                    int end = line.lastIndexOf(']');
                    if (start != -1 && end != -1) {
                        mLrcList.add(line.substring(end + 1));
                        mTimeList.add(getMillSeconds(line.substring(start + 1, end)));
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                CloseUtils.close(reader);
            }
        }
    }

    private long getMillSeconds(String format) {
        int start = format.indexOf(':');
        int end = format.lastIndexOf('.');
        long mimute = Long.parseLong(format.substring(0, start));
        long second = Long.parseLong(format.substring(start + 1, end));
        long millSecond = Long.parseLong(format.substring(end + 1));
        return mimute * 60l * 1000l + second * 1000l + millSecond;
    }
}
